home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 2 / Apprentice-Release2.iso / Source Code / C / Games / MacGnuGo 0.5e / gnugo.src / macif.c < prev    next >
Encoding:
Text File  |  1993-11-17  |  10.5 KB  |  489 lines  |  [TEXT/R*ch]

  1. /* macif.c - macintosh interface routines - rhn@netcom.com  */
  2.  
  3. void (*OnQuit)(), (*OnMouse)();
  4. extern void do_pass(), on_undo();
  5. extern int on_black(), on_white(), on_bigger(), on_smaller(), on_more(), on_less();
  6.  
  7. char *AboutMenu = "About ...;-";
  8. char *AboutText = "93Oct01";
  9.  
  10. int sound_ok = 1;
  11. int randomize_flag = 0;
  12. void macinit(char *d1,int x,int y);    /* initialization    */
  13. void DoEvent();
  14.  
  15. #include <Types.h>
  16. #include <QuickDraw.h>
  17. #include <Fonts.h>
  18. #include <Events.h>
  19. #include <Windows.h>
  20. #include <Menus.h>
  21. #include <TextEdit.h>
  22. #include <StandardFile.h>
  23. #include <Memory.h>
  24. #include <Files.h>
  25. #include <GestaltEqu.h>
  26.  
  27. #define MenuBarHeight    20
  28. #define ScreenMargin    4
  29.  
  30. #define appleID        1
  31. #define fileID        256
  32. #define editID        257
  33. #define controlID    258
  34.  
  35. static void SetupMenus();
  36. static void DoMouseDown();
  37. static void DoMenuClick();
  38. static void DoDrag();
  39. static void DoGoAway();
  40. static void DoContent();
  41. static void DoKeyPress();
  42. static void DoActivate();
  43. static void DoUpdate();
  44. static void DoMenuCommand();
  45. static void DoAppleMenu();
  46. static void DoFileMenu();
  47. static void DoEditMenu();
  48. static void DoControlMenu();
  49.  
  50. extern char *PtoCstr();
  51. static void none() { return; }     /* null function */ 
  52.  
  53. int screenWidth;
  54. int screenHeight;
  55. int nHorizontal,nVertical,nWidth,nHeight;
  56. static WindowRecord myWrecord;
  57. WindowPtr myWindow;
  58. static CursHandle myCursor;
  59.  
  60. static MenuHandle appleMenu;
  61. static MenuHandle fileMenu;
  62. static MenuHandle editMenu;
  63. static MenuHandle controlMenu;
  64.  
  65. static long    gestaltflag;
  66.  
  67. void macinit(WinTitle ,winx, winy)
  68. char *WinTitle;
  69. int winx,winy;
  70. {
  71.     Rect     myBoundsRect;
  72.     
  73.     MaxApplZone();
  74.     InitGraf(&thePort);
  75.     InitFonts();
  76.     FlushEvents (everyEvent,0);  /* ignore left over events */
  77.     InitWindows();
  78.     InitMenus();
  79.     TEInit();
  80.     InitDialogs(0L);
  81.     InitCursor();
  82.  
  83.     SetupMenus();
  84.  
  85.     screenWidth  = screenBits.bounds.right  - screenBits.bounds.left;
  86.     screenHeight = screenBits.bounds.bottom - screenBits.bounds.top;
  87.  
  88.     SetRect(&myBoundsRect,16, 42, 48, 74);
  89.     myWindow = NewWindow(&myWrecord,&myBoundsRect,CtoPstr(WinTitle),
  90.                         FALSE,documentProc,(WindowPtr)-1,TRUE,0);
  91.     SetPort(myWindow);
  92.  
  93.     TextFont(4); TextSize(9); TextMode(srcCopy);
  94.     SetFontLock(-1);
  95.     InitWin(winx,winy);
  96.  
  97.     if ( (Gestalt(gestaltSystemVersion, &gestaltflag) != noErr) ||
  98.         ((gestaltflag & 0xffff) < 0x0605) ) {
  99.     gestaltflag = 0;
  100.     }
  101.     
  102.     OnQuit=none; OnMouse=none;
  103.  
  104.     FlushEvents (everyEvent,0);
  105. }
  106.  
  107. static void SetupMenus()
  108. {
  109.     appleMenu = NewMenu(appleID,CtoPstr("\24"));
  110.     AppendMenu(appleMenu, CtoPstr(AboutMenu));
  111.     AddResMenu(appleMenu,'DRVR');
  112.     InsertMenu(appleMenu,0);
  113.     
  114.     fileMenu = NewMenu(fileID,CtoPstr("File")); /* the file menu */
  115.     AppendMenu(fileMenu,
  116.         CtoPstr("New Game/N;-;Save;Save as...;-;Print;-;Quit/Q"));
  117.     InsertMenu(fileMenu,0);
  118.     DisableItem(fileMenu,3);
  119.     DisableItem(fileMenu,4);
  120.     DisableItem(fileMenu,6);
  121.     
  122.     editMenu = NewMenu(editID,CtoPstr("Edit"));    /* the edit menu */
  123.     AppendMenu(editMenu, CtoPstr("Undo/Z;-;Cut/X;Copy/C;Paste/V;Clear"));
  124.     InsertMenu(editMenu,0);
  125.  
  126.     controlMenu = NewMenu(controlID,CtoPstr("Control"));
  127.     AppendMenu(controlMenu,
  128.       CtoPstr(
  129.         "pass/P;Sound;-;play black;play white;play both;bigger game/B;smaller game/S;more handicap/M;less handicap/L;randomize"));
  130.     InsertMenu(controlMenu,0);
  131.     CheckItem(controlMenu,2,sound_ok);
  132.     CheckItem(controlMenu,5,1);
  133.  
  134.     DrawMenuBar();
  135. }
  136.  
  137. void Idle(t)
  138. int t;
  139. {
  140.     EventRecord    myEvent;
  141.     long int t0, t1;
  142.     t0 = TickCount();
  143.     t1 = t0 + t;
  144.     while (TickCount() < t1) {
  145.       SystemTask();
  146.       if (gestaltflag == 0) GetNextEvent(0,&myEvent);
  147.       else           WaitNextEvent(0,&myEvent,0L,0L);
  148.     }
  149. }
  150.  
  151. void DoEvent()
  152. {
  153.     EventRecord    myEvent;
  154.     Point    aPoint;
  155.     
  156.     SystemTask();
  157.  
  158.     while (  gestaltflag == 0 ?
  159.                GetNextEvent(everyEvent,&myEvent) :
  160.                WaitNextEvent(everyEvent,&myEvent,0L,0L)  ) {
  161.     switch (myEvent.what) {
  162.         case mouseDown:
  163.         DoMouseDown(&myEvent);
  164.         break;
  165.         case keyDown:
  166.         case autoKey:
  167.         DoKeyPress(&myEvent);
  168.         break;
  169.         case activateEvt:
  170.         DoActivate(&myEvent);
  171.         break;
  172.         case updateEvt:
  173.         DoUpdate(&myEvent);
  174.         break;
  175.         case diskEvt:
  176.         if ( HiWord(myEvent.message) != noErr ) {
  177.           SetPt(&aPoint, 0x070, 0x050);
  178.           DIBadMount(aPoint, myEvent.message);
  179.         }
  180.         break;
  181.         }
  182.     }
  183. }
  184.  
  185. static void DoMouseDown(myEvent)
  186.   EventRecord *myEvent;
  187. {
  188.     WindowPtr whichWindow;
  189.  
  190.     switch (FindWindow(myEvent->where,&whichWindow)) {
  191.     case inMenuBar:
  192.     DoMenuClick(myEvent);
  193.     break;
  194.     case inSysWindow:
  195.     SystemClick(myEvent,whichWindow);
  196.     break;
  197.     case inDrag:
  198.     DoDrag(myEvent,whichWindow);
  199.     break;
  200.     case inGoAway:
  201.     DoGoAway(myEvent,whichWindow);
  202.     break;
  203.     case inGrow:
  204.     break;
  205.     case inContent:
  206.     DoContent(myEvent,whichWindow);
  207.     break;
  208.     }
  209. }
  210.  
  211. static void DoMenuClick(myEvent)
  212.   EventRecord *myEvent;
  213. {
  214.     long choice;
  215.     if (choice = MenuSelect(myEvent->where))
  216.     DoMenuCommand(choice);
  217. }
  218.  
  219. static void DoDrag(myEvent,whichWindow)
  220.   EventRecord *myEvent;
  221.   WindowPtr whichWindow;
  222. {
  223.     Rect dragRect;
  224.     SetRect(&dragRect,0,MenuBarHeight,screenWidth,screenHeight);
  225.     InsetRect(&dragRect,ScreenMargin,ScreenMargin);
  226.     DragWindow(whichWindow,myEvent->where,&dragRect);
  227. }
  228.  
  229. static void DoGoAway(myEvent,whichWindow)
  230.   EventRecord *myEvent;
  231.   WindowPtr whichWindow;
  232. {
  233.     if (TrackGoAway(whichWindow,myEvent->where)) {
  234.     if (OnQuit != none) (*OnQuit)();
  235.     else {
  236.           TEToScrap();
  237.       ExitToShell();
  238.     }
  239.     }
  240. }
  241.  
  242. static void DoContent(myEvent,whichWindow)
  243.   EventRecord *myEvent;
  244.   WindowPtr whichWindow;
  245. {
  246.    Point clickLoc;
  247.     
  248.     if (whichWindow != FrontWindow())
  249.     SelectWindow(whichWindow);
  250.     else {
  251.       if (whichWindow == myWindow)
  252.         if (OnMouse != none) (*OnMouse)(clickLoc.h, clickLoc.v, myEvent->modifiers);
  253.         else {
  254.           myCursor = GetCursor (watchCursor);  SetCursor (*myCursor);
  255.          clickLoc = myEvent->where;
  256.          GlobalToLocal(&clickLoc);
  257.           clicktask(clickLoc.h, clickLoc.v, myEvent->modifiers);
  258.           InitCursor();
  259.         } 
  260.       }
  261. }
  262.  
  263. static void DoKeyPress(myEvent)
  264.   EventRecord *myEvent;
  265. {
  266.     long choice;
  267.     
  268.     if (FrontWindow() == myWindow) {
  269.     if (myEvent->modifiers & 0x100) {
  270.         if (choice = MenuKey((char)myEvent->message))
  271.         DoMenuCommand(choice);
  272.         }
  273.     else if ((choice = (char)myEvent->message) == 32 ||
  274.               choice == 'p' || choice == 'P' || choice == 13) {
  275.       myCursor = GetCursor (watchCursor);  SetCursor (*myCursor);
  276.           do_pass();
  277.           InitCursor();
  278.         }
  279.         else MyBeep(2);
  280.         }
  281. }
  282.  
  283. static void DoActivate(myEvent)
  284.   EventRecord *myEvent;
  285. {
  286.     WindowPtr whichWindow;
  287.     whichWindow = (WindowPtr)myEvent->message;
  288.     SetPort(whichWindow);
  289.     if (whichWindow == myWindow)
  290.     DrawGrowIcon(whichWindow);
  291. }
  292.  
  293. static void DoUpdate(myEvent)
  294.   EventRecord *myEvent;
  295. {
  296.     WindowPtr whichWindow;
  297.     GrafPtr savePort;
  298.     GetPort(&savePort);
  299.     whichWindow = (WindowPtr)myEvent->message;
  300.     SetPort(whichWindow);
  301.     BeginUpdate(whichWindow);
  302.     EraseRect(&whichWindow->portRect);
  303.     if (whichWindow == myWindow) {
  304.     DrawGrowIcon(whichWindow);
  305.     RedrawScreen();
  306.     }
  307.     EndUpdate(whichWindow);
  308.     SetPort(savePort);
  309. }
  310.  
  311. static void DoMenuCommand(choice)
  312.   long choice;
  313. {
  314.     short theMenu,theItem;
  315.     
  316.     theMenu = HiWord(choice);
  317.     theItem = LoWord(choice);
  318.     
  319.     HiliteMenu(theMenu);
  320.     switch (theMenu) {
  321.     case appleID:
  322.     DoAppleMenu(theItem);
  323.     break;
  324.     case fileID:
  325.     DoFileMenu(theItem);
  326.     break;
  327.     case editID:
  328.     DoEditMenu(theItem);
  329.     break;
  330.     case controlID:
  331.     DoControlMenu(theItem);
  332.     break;
  333.     }
  334.     HiliteMenu(0);
  335. }
  336.  
  337. static void DoAppleMenu(theItem)
  338.   short theItem;
  339. {
  340.     unsigned char name[256];
  341.     GrafPtr gp;
  342.  
  343.     switch (theItem) {
  344.     case 1:
  345.     doaboutbox(AboutText);
  346.     break;
  347.     default:
  348.     GetItem(appleMenu,theItem, name);
  349.     GetPort(&gp);
  350.     OpenDeskAcc(name);
  351.     SetPort(gp);
  352.     break;
  353.     }
  354. }
  355.  
  356. static void DoFileMenu(theItem)
  357.   int theItem;
  358. {
  359.     switch (theItem) {
  360.     case 1:    /*  */
  361.     on_new();
  362.     break;
  363.     case 8:    /* Quit */
  364.     if (OnQuit != none) (*OnQuit)();
  365.     else {
  366.       ExitToShell();
  367.     }
  368.     break;
  369.     default:
  370.     break;
  371.     }
  372. }
  373.  
  374. static void DoEditMenu(theItem)
  375.   int theItem;
  376. {
  377.   if (SystemEdit(theItem-1)) return;
  378.   else switch (theItem) {
  379.     case 1:    /*  */
  380.     on_undo();
  381.     break;
  382.     default:
  383.     MyBeep(2);
  384.     break;
  385.   }
  386. }
  387.  
  388. static void DoControlMenu(theItem)
  389.   int theItem;
  390. {
  391.     HiliteMenu(0);
  392.     switch (theItem) {
  393.     case 1:
  394.         myCursor = GetCursor (watchCursor);  SetCursor (*myCursor);
  395.     do_pass();
  396.         InitCursor();
  397.     break;
  398.     case 2:    /* continue */
  399.         sound_ok = !sound_ok;
  400.         CheckItem(controlMenu,2,sound_ok);
  401.     break;
  402.     case 4:    /*  */
  403.         on_black();
  404.         CheckItem(controlMenu,4,1);
  405.         CheckItem(controlMenu,5,0);
  406.         CheckItem(controlMenu,6,0);
  407.     break;
  408.     case 5:    /*  */
  409.     on_white();
  410.         CheckItem(controlMenu,4,0);
  411.         CheckItem(controlMenu,5,1);
  412.         CheckItem(controlMenu,6,0);
  413.           break;
  414.     case 6:    /*  */
  415.     on_both();
  416.         CheckItem(controlMenu,4,0);
  417.         CheckItem(controlMenu,5,0);
  418.         CheckItem(controlMenu,6,1);
  419.           break;
  420.     case 7:    /*  */
  421.     on_bigger();
  422.     break;
  423.     case 8:    /*  */
  424.     on_smaller();
  425.     break;
  426.     case 9:    /*  */
  427.     on_more();
  428.     break;
  429.     case 10:    /*  */
  430.     on_less();
  431.     break;
  432.     case 11:    /*  */
  433.         randomize_flag = !randomize_flag;
  434.         CheckItem(controlMenu,11,randomize_flag);
  435.         on_randomize(randomize_flag);
  436.     break;
  437.     }
  438. }
  439.  
  440. InitWin(xx,yy)
  441. int xx,yy;
  442. {
  443.     Rect rect;
  444.  
  445.     nHorizontal = 20;
  446.     nVertical = 20 + 2 * MenuBarHeight;
  447.     nWidth = xx;
  448.     nHeight = yy;
  449.     ShowHide(myWindow,0);
  450.     MoveWindow(myWindow,nHorizontal,nVertical,-1);
  451.     SizeWindow(myWindow,nWidth,nHeight,-1);
  452.     InvalRect(&myWindow->portRect);
  453.     ShowHide(myWindow,-1);
  454.     rect = myWindow->portRect;
  455.     EraseRect(&rect);
  456. }
  457.  
  458. MyBeep(int i) { if (sound_ok) SysBeep(i); }
  459.  
  460. int mystrlen(s) char *s;{ int i; for (i=0; s[i] != 0; i++); return(i); }
  461.  
  462. DrawCStr(char *s) { DrawText(s,0,mystrlen(s)); }
  463.  
  464. DrawNum1(int n) { if (n > 0) { DrawNum1(n/10); DrawChar('0'+(n % 10)); }}
  465. DrawNum(int n) { DrawNum1(n/10); DrawChar('0'+(n % 10)); }
  466.  
  467. doaboutbox(s)
  468. char *s;
  469. {
  470.   Rect r;
  471.   WindowPtr wp;
  472.   WindowRecord wr;
  473.   SetRect(&r,200, 100, 500, 200);
  474.   wp = NewWindow(&wr,&r,"\pAbout ...",
  475.                         TRUE,dBoxProc,(WindowPtr)-1,FALSE,0);
  476.   if (wp == NULL) ExitToShell();
  477.   SelectWindow(wp);
  478.   SetPort(wp); TextFont(1); TextSize(12);
  479.   MoveTo(20,30); DrawCStr(s);
  480.   MoveTo(20,46); DrawCStr("mac port, rhn@netcom.com");
  481.   MoveTo(20,62); DrawCStr("see file README for credits");
  482.   MoveTo(20,78); DrawCStr("see FSF file COPYING for copyright");
  483.   while (!Button()) SystemTask();
  484.   FlushEvents (everyEvent,0);
  485.   CloseWindow(wp);
  486.   SetPort(myWindow);
  487. }
  488.  
  489. /* end */